package com.lanhai.mind.dispatch;

import com.lanhai.mind.beans.AgvBean;
import com.lanhai.mind.beans.DotBean;
import com.lanhai.mind.beans.InnerBean;
import com.lanhai.mind.interfaces.IAgv; 
import com.lanhai.mind.interfaces.IAgvEvent;
import com.lanhai.mind.interfaces.IAgvMount;
import com.lanhai.mind.interfaces.IDot;
import com.lanhai.mind.interfaces.IPower;
import com.lanhai.mind.interfaces.ITempData;

/**
 * 内部调度
 * @author zhengwei
 *
 */
public class DispatchInner {
	IDot iDot;
	IAgvMount iAgvMount;
	ITempData iTempData;
	IAgv iAgv;
	IPower iPower;
	IAgvEvent iAgvEvent; 
	
	void Resolve(InnerBean bean) {
		switch(bean.getCmd()) {
		case Judgement:
			//传输调度
			iAgvMount.judgement(bean.getAgvid() , bean.getDeviceid());
			break; 
		case NoticeDeviceDot:
			////释放了对接设备，分配下一小车进入
			//通知对应的装卸分配点有空机台可用
			 DotBean rdot=iDot.getBean(bean.getDotNo());
			 if(rdot==null) {
				//错误的点号
				 return;
			 } 
			 
			//通知小车去传输位 
			 int sagvid=iTempData.getAgvIdByDotNo(bean.getDotNo());
			 AgvBean ag =iAgv.getBean(sagvid);
			  if(ag!=null) {
				  //通知小车去机台
				  //TODO 找机台，放小车过去
				  /*
				  iTempData.lockRoad(rdot.getTargetRoad(), true);
				  RoadCache.lockRoad(dot.getTargetRoad(), true); 
					AgvResolve.AgvToRoad(ag, dot.getTargetRoad()); 
					*/
			  }
			  
			//看看有没有充够电的充电桩
			int powp=iPower.getPowerFullDotNo(20);
			if(powp>0) {
				 int agp =iTempData.getAgvIdByDotNo(powp);
				 AgvBean powagv=iAgv.getBean(agp); 
				if(powagv!=null) {
					//释放充电桩
					iPower.lockPower(powp, false);
 
					//回到主路
					iAgvEvent.toRoad(powagv,   0); 
				} 
			} 
			break;
		case NoticeEstimate:
			////通知预判断点放行小车
		 
			int agvid=iTempData.getNextAgvOfEstimate(bean.getDotNo()); 
			if(agvid==0) {
				//没有野车
				return;
			}
			//让该小车进入装卸判断位
			AgvBean agv2=iAgv.getBean(agvid); ;
			if(agv2!=null) {
				DotBean tDot=iDot.getBean(bean.getDotNo());
				if(tDot.getTargetRoad()>0) { 
					 iTempData.lockRoad(tDot.getTargetRoad(), true);
					 iAgvEvent.toRoad(agv2,   tDot.getTargetRoad()); 
				} 
			}
			
			break; 
		default:
			break;
		
		}
	}
	
}
